<!-- 甲方客户管理 -->
<template>
    <div >
        <div class='table-page-search-wrapper'>
            <a-form layout='inline'>
               <a-row :gutter='48'>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="公司名称">
                            <a-input v-model="customerName" placeholder="请输入公司名称" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="统一社会信用代码">
                            <a-input v-model="socialCode" placeholder="请输入统一社会信用代码" allow-clear></a-input>
                        </a-form-item>
                    </a-col><a-col :md='8' :sm='24'>
                        <a-form-item label="手机号">
                            <a-input v-model="contactPhone" placeholder="请输入手机号" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="姓名">
                            <a-input v-model="contactName" placeholder="请输入姓名" allow-clear></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md='8' :sm='24'>
                        <a-form-item label="状态">
                            <a-select v-model="state" placeholder="请选择">
                                <a-select-option v-for="item in statusList" :key="item.id" :value="item.id">{{item.name}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md='16' :sm='24'>
                        <a-form-item label="创建时间">
                            <a-range-picker :show-time="{ format: 'HH:mm:ss' }" format="YYYY-MM-DD HH:mm:ss" @ok="onDateChange" allow-clear/>
                        </a-form-item>
                    </a-col>
                    <a-col :md='4' :sm='24'>
                        <a-form-item >
                            <a-button type='primary' @click="getData">查询</a-button>
                            <!-- <a-button style="margin-left: 20px" type='' @click="add">添加客户</a-button> -->
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>  

        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ 'total': total ,'showSizeChanger': true,'pageSize': size,}" size="small" :scroll="{ x: 1200, }">
            <div slot="state" slot-scope="state">
                <a-badge :status="state | statusTypeFilter" :text="state | statusFilter" />
            </div>
            <div slot="operation" slot-scope="text,record">
                <div class="editable-row-operations">
                    <a-button type="link" v-if="record.state == 0" @click="updateState(record,1)">启用</a-button>
                    <a-button type="link" v-else @click="updateState(record,0)">停用</a-button>
                </div>
            </div>
        </a-table>
    </div>
</template>

<script>
import { columns } from './table';
import { queryCustomerList , updateCustomerState } from '@/api/customer';
export default {
    data () {
        return {
            customerName: '', // 公司名称
            socialCode: '', // 统一社会信用代码
            contactName: '', // 姓名
            contactPhone: '', // 手机号
            columns,
            page: 1,
            size: 10,
            total: 0,
            dataList: [],
            isload: false,
            state: undefined,
            statusList: [
                { id: 0, name: '已停用'},
                { id: 1, name: '使用中'},
            ],
        };
    },
    created(){
        this.getData();
    },
    methods: {
        // 日期筛选
        onDateChange(date,dateStr){
            console.log(date,dateStr)
            if(date.length > 0){
                this.startTime = date[0].format('YYYY-MM-DD HH:mm:ss')
                this.endTime = date[1].format('YYYY-MM-DD HH:mm:ss')
            }else{
                this.startTime = ''
                this.endTime = ''
            }
            this.getData()
        },
        // 获取账户列表数据
        getData(){
            let _this = this
            _this.isload = true;
            const params = {
                'customerName': _this.customerName, // 公司名称
                'socialCode': _this.socialCode, 
                'contactName': _this.contactName,
                'contactPhone': _this.contactPhone,
                'state': _this.state,
                'page': _this.page - 1,
                'size': _this.size,
            }
            queryCustomerList(params)
            .then(res=>{
                _this.isload = false;
                if(res.success){
                    _this.dataList = res.data;
                    _this.total = res.totalCount;
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 状态切换
        updateState(item,state){
            let _this = this
            _this.$confirm({
                title: '温馨提示',
                content: state == 1 ? '是否确认开启当前客户' : '是否确认关闭当前客户',
                okText: state == 1 ? '确认开启' : '确认关闭',
                onOk() {
                    const params = {
                        'customerId': item.id,
                        'state': state,
                    }
                    updateCustomerState(params)
                    .then(res=>{
                        if(res.success){
                            _this.$notification['success']({
                                message: '温馨提示',
                                description: state == 1 ? '开启成功' : '关闭成功',
                                duration: 8
                            })
                            _this.getData()
                        }else{
                            _this.$notification['error']({
                                message: '温馨提示',
                                description: res.errMessage,
                                duration: 8
                            })
                        }
                    })
                },
                onCancel() {},
            });
        },
    }
}

</script>
<style scoped>

</style>